<template>
  <div class="login-container">
    <!-- 左侧品牌区 -->
    <div class="brand-side">
      <div class="brand-content">
        <img 
          src="../../picture/生成校园小程序 logo.png"
          class="brand-logo"
          alt="校园帮logo"
        />
        <h1 class="brand-title">校园帮</h1>
        <p class="brand-desc">智慧校园生活平台</p>
      </div>
    </div>

    <!-- 右侧注册区 -->
    <div class="login-form-container">
      <div class="login-card">
        <div class="login-form">
          <div class="form-header">
            <h2 class="login-title">账号注册</h2>
            <div class="title-divider"></div>
          </div>

          <el-form ref="formRef" :model="registerForm" :rules="rules" class="form-content">
            <!-- 用户名 -->
            <el-form-item prop="username" class="input-item">
              <el-input 
                v-model="registerForm.username"
                placeholder="请输入用户名"
                class="custom-input"
              ></el-input>
            </el-form-item>

            <!-- 账号 -->
            <el-form-item prop="account" class="input-item">
              <el-input 
                v-model="registerForm.account"
                placeholder="请输入账号"
                class="custom-input"
              ></el-input>
            </el-form-item>

            <!-- 密码 -->
            <el-form-item prop="password" class="input-item">
              <el-input 
                v-model="registerForm.password"
                placeholder="请设置密码(至少6位)"
                type="password"
                class="custom-input"
              ></el-input>
            </el-form-item>

            <!-- 确认密码 -->
            <el-form-item prop="confirmPassword" class="input-item">
              <el-input 
                v-model="registerForm.confirmPassword"
                placeholder="请确认密码"
                type="password"
                class="custom-input"
              ></el-input>
            </el-form-item>

            <!-- 手机号 -->
            <el-form-item prop="phone" class="input-item">
              <el-input 
                v-model="registerForm.phone"
                placeholder="请输入手机号"
                class="custom-input"
              ></el-input>
            </el-form-item>

            <el-button type="primary" class="login-btn" @click="handleRegister">
              立即注册
            </el-button>

            <div class="register-link">
              <span>已有账号？</span>
              <router-link to="/login" class="register-text">立即登录</router-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { successAlert, errorAlert } from '@/utils/alert'

const router = useRouter()
const formRef = ref(null)

const registerForm = ref({
  username: '', // 新增用户名字段
  account: '',
  password: '',
  confirmPassword: '',
  phone: ''
})

const rules = {
  username: [ // 新增用户名验证规则
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 10, message: '用户名长度2-10位', trigger: 'blur' }
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 16, message: '账号长度3-16位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        // Use registerForm directly instead of rule.form
        if (value !== registerForm.value.password) {
          callback(new Error('两次密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  try {
    await formRef.value.validate()
    const baseUrl = process.env.VUE_APP_API_BASE.trim();
    const response = await axios.post(`${baseUrl}/api/auth/register`, registerForm.value)

    if (response.data.success) {
      successAlert('注册成功，请登录')
      router.push('/login')
    } else {
      // 直接显示后端返回的具体错误信息
      errorAlert(response.data.message)
    }
  } catch (error) {
    console.error('注册失败:', error)
    const msg = error.response?.data?.message || '网络错误，请稍后重试'
    errorAlert(msg)
  }
}
</script>

<style scoped>
/* 基础布局样式 */
.login-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="%23ffffff" opacity="0.05"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  pointer-events: none;
}

.brand-side {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  position: relative;
  z-index: 1;
}

.brand-content {
  text-align: center;
}

.brand-logo {
  width: 120px;
  height: 120px;
  margin-bottom: 30px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  padding: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  object-fit: contain;
}

.brand-logo:hover {
  transform: scale(1.05);
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.15);
}

.brand-title {
  font-size: 48px;
  margin-bottom: 20px;
  font-weight: 800;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  background: linear-gradient(45deg, #ffffff, rgba(255, 255, 255, 0.8));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.brand-desc {
  font-size: 18px;
  opacity: 0.9;
  font-weight: 400;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 300px;
  line-height: 1.6;
  margin: 0 auto;
}

.login-form-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  position: relative;
  z-index: 1;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 50px 40px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 24px;
  pointer-events: none;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 35px 70px rgba(0, 0, 0, 0.2);
}

.form-header {
  text-align: center;
  margin-bottom: 28px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  position: relative;
  z-index: 1;
  margin: 0 0 12px 0;
}

.title-divider {
  width: 60px;
  height: 3px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  margin: 0 auto;
  border-radius: 2px;
}

/* 输入框核心样式（修复黑框） */
.input-item {
  margin-bottom: 20px !important;
}

:deep(.custom-input .el-input__wrapper) {
  border-radius: 16px !important;
  background: rgba(255, 255, 255, 0.8) !important;
  backdrop-filter: blur(10px) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  padding: 0 16px !important;
  height: 50px !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05) !important;
  outline: none !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  box-sizing: content-box !important;
  overflow: visible !important;
}

:deep(.custom-input .el-input__wrapper:focus-within) {
  border-color: #667eea !important;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15) !important, 0 8px 25px rgba(0, 0, 0, 0.1) !important;
  background: rgba(255, 255, 255, 0.95) !important;
  transform: translateY(-2px) !important;
  outline: none !important;
}

:deep(.custom-input .el-input__wrapper:hover:not(.is-focus)) {
  border-color: rgba(102, 126, 234, 0.4) !important;
  background: rgba(255, 255, 255, 0.9) !important;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1) !important;
  transform: translateY(-1px) !important;
}

:deep(.el-form-item.is-error .custom-input .el-input__wrapper) {
  border-color: #f56c6c !important; /* 错误红色 */
  box-shadow: none !important;
}

:deep(.custom-input .el-input__inner) {
  font-size: 14px !important;
  line-height: 50px !important;
  border: none !important; /* 移除内部边框 */
  background: transparent !important;
  outline: none !important; /* 内部输入框也无轮廓 */
}

:deep(.el-form-item__error) {
  margin-top: 8px !important;
  padding-left: 16px !important;
  font-size: 12px !important;
  color: #f56c6c !important;
}

/* 按钮样式 */
:deep(.login-btn) {
  width: 100% !important;
  height: 50px !important;
  border-radius: 16px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3) !important;
  position: relative !important;
  z-index: 1 !important;
  overflow: hidden !important;
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.login-btn:hover) {
  transform: translateY(-3px) !important;
  box-shadow: 0 15px 35px rgba(102, 126, 234, 0.4) !important;
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #606266;
}

.register-text {
  color: #667eea;
  margin-left: 5px;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
}

.register-text::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.register-text:hover {
  color: #764ba2;
}

.register-text:hover::after {
  width: 100%;
}

/* 响应式 */
@media (max-width: 992px) {
  .brand-side {
    display: none;
  }
  .login-form-container {
    flex: 1;
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .brand-side {
    flex: 0 0 200px;
    padding: 20px;
  }
  
  .brand-logo {
    width: 80px;
    height: 80px;
  }
  
  .brand-title {
    font-size: 32px;
  }
  
  .login-form-container {
    padding: 20px;
  }
  
  .login-card {
    padding: 30px 20px;
  }
}
</style>